Ext.define('YXMES.sres.controller.DeviceResourceController', {
	extend : 'Ext.app.Controller',
	refs : [ {
		selector : 'sres_deviceresource',
		ref : 'sresdeviceresource'
	} ],
	requires : [ 'YXMES.sres.store.ResStore', 'YXMES.sres.store.ResTypeStore', 'YXMES.sres.store.WorkshopStore', 'YXMES.sres.store.LineStore', 'YXMES.sres.store.UserStore' ],
	stores : [],
	models : [],
	views : [ 'YXMES.sres.view.DeviceResourceView' ],
	subcontrollers : [  ],
	_init : false,
	init : function() {
		if (this._init)
			return;
		this._init = true;
		this.control({
			'sres_deviceresource' : {
				afterrender : function() {
					return this.handle(this.onAfterrender, arguments);
				},
				destroy : function() {
					return this.handle(this.onDestroy, arguments);
				}
			},
			'sres_deviceresource #btnSave' : {
				click : function() {
					return this.handle(this.onBtnSaveClick, arguments);
				}
			},
			'sres_deviceresource #btnDel' : {
				click : function() {
					return this.handle(this.onBtnDelClick, arguments);
				}
			},
			'sres_deviceresource #btnSearch' : {
				click : function() {
					return this.handle(this.onBtnSearchClick, arguments);
				}
			},
			'sres_deviceresource #btnReset' : {
				click : function() {
					return this.handle(this.onBtnResetClick, arguments);
				}
			},
			'sres_deviceresource #grdRes' : {
				itemclick : function() {
					return this.handle(this.onGrdResItemclick, arguments);
				}
			},
			'sres_deviceresource #cmbShopCode' : {
				select : function() {
					return this.handle(this.onCmbShopCodeSelect, arguments);
				}
			}
		});
		if (!Ext.isEmpty(this.subcontrollers)) {
			Ext.each(this.subcontrollers, function(name) {
				this.getController(name).init();
			}, this);
		}
		// User definied init
	},
	mainView : undefined,
	getMainView : function() {
		return this.mainView;
	},
	getScopeView : function() {
		return this.getMainView().scopeView || {};
	},
	getInput : function() {
		return this.getScopeView().input || {};
	},
	setInput : function(data) {
		this.getMainView().input = data;
	},
	getOutput : function() {
		return this.getMainView().output || {};
	},
	setOutput : function(data) {
		this.getScopeView().output = data;
	},
	handle : function(func, args) {
		var item = args && args.length > 0 && Ext.typeOf(args[0]) === 'object' ? args[0] : undefined;
		if (item) {
			try {
				this.mainView = (item.xtype == 'sres_deviceresource' ? item : item.up('sres_deviceresource'));
			} catch(e) {
			}
		}
		try {
			return func.apply(this, args);
		} finally {
		}
	},
	getItem : function(itemId) {
		return this.getMainView().down('#' + itemId);
	},
	getStore : function(name) {
		if (name == 'ResStore')
			return this.getMainView().SresViewDeviceResourceServiceStore;
		if (name == 'ResTypeStore')
			return this.getMainView().BasViewCommonDataSetupDetailServiceStore;
		if (name == 'WorkshopStore')
			return this.getMainView().SbasViewWorkshopServiceStore;
		if (name == 'LineStore')
			return this.getMainView().SbasViewProductionLineServiceStore;
		if (name == 'StatusStore')
			return this.getMainView().statusStoreStore;
		if (name == 'UserStore')
			return this.getMainView().SecViewUserInfoServiceStore;
		return undefined;
	},
	bind : function(view) {
		view = (view ? view : this.getMainView());
		view.SresViewDeviceResourceServiceStore = Ext.create('YXMES.sres.store.ResStore', {
			pageSize : 50
		});
		view.BasViewCommonDataSetupDetailServiceStore = Ext.create('YXMES.sres.store.ResTypeStore', {
			pageSize : 30
		});
		view.SbasViewWorkshopServiceStore = Ext.create('YXMES.sres.store.WorkshopStore', {
			pageSize : 30
		});
		view.SbasViewProductionLineServiceStore = Ext.create('YXMES.sres.store.LineStore', {
			pageSize : 30
		});
		view.statusStoreStore = Ext.create('Ext.data.ArrayStore', {
			pageSize : 30,
			autoDestory : true,
			fields : [ {
				name : 'value',
				type : 'string'
			}, {
				name : 'text',
				type : 'string'
			} ],
			data : [
				[ 'N', '正常' ],
				[ 'D', '删除' ],
				[ 'F', '禁用' ]
			]
		});
		view.SecViewUserInfoServiceStore = Ext.create('YXMES.sres.store.UserStore', {
			pageSize : 30,
			autoDestory : true
		});
		view.down('#grdRes').reconfigure(view.SresViewDeviceResourceServiceStore);
		view.down('#bar5').bindStore(view.SresViewDeviceResourceServiceStore);
		view.down('#cmbShopCode').bindStore(view.SbasViewWorkshopServiceStore);
		view.down('#cmbLineCode').bindStore(view.SbasViewProductionLineServiceStore);
		view.down('#cmbSearchResType').bindStore(view.BasViewCommonDataSetupDetailServiceStore);
		view.down('#cmbResType').bindStore(view.BasViewCommonDataSetupDetailServiceStore);
		view.down('#cmbResStatus').bindStore(view.statusStoreStore);
		view.down('#cdvMaintainUser').bindStore(view.SecViewUserInfoServiceStore);
	},
	onAfterrender : function(view, eOpts) {
		this.bind();
		// User defined afterrender
		this.loadResTypeStore({groupName:'RES_TYPE'});
		this.loadWorkshopStore();
	},
	onDestroy : function(view, eOpts) {
		// User defined destory
		var view = this.getMainView();
		if (view.SresViewDeviceResourceServiceStore) {
			view.SresViewDeviceResourceServiceStore.destroyStore();
			view.SresViewDeviceResourceServiceStore = undefined;
		}
		if (view.BasViewCommonDataSetupDetailServiceStore) {
			view.BasViewCommonDataSetupDetailServiceStore.destroyStore();
			view.BasViewCommonDataSetupDetailServiceStore = undefined;
		}
		if (view.SbasViewWorkshopServiceStore) {
			view.SbasViewWorkshopServiceStore.destroyStore();
			view.SbasViewWorkshopServiceStore = undefined;
		}
		if (view.SbasViewProductionLineServiceStore) {
			view.SbasViewProductionLineServiceStore.destroyStore();
			view.SbasViewProductionLineServiceStore = undefined;
		}
		if (view.statusStoreStore) {
			view.statusStoreStore.destroyStore();
			view.statusStoreStore = undefined;
		}
		if (view.SecViewUserInfoServiceStore) {
			view.SecViewUserInfoServiceStore.destroyStore();
			view.SecViewUserInfoServiceStore = undefined;
		}
		if (this.getMainView().input)
			this.getMainView().input = undefined;
		if (this.getMainView().output)
			this.getMainView().output = undefined;
	},
	onBtnSaveClick : function(button, e, eOpts) {
		if (!this.getItem('frmOper').isValid()) {
		    return;
		}
		
		var me = this;
		Ext.Msg.show({
		    title: button.getText(),
		    msg: T.msg.are_you_sure_to_save_data,
		    icon: Ext.Msg.QUESTION,
		    buttons: Ext.Msg.YESNO,
		    fn: function(btnId) {
		        if ('yes' == btnId) {
		            button.setDisabled(true);
		
		            YX.ZbusRpc.request({
		                module: 'SresSaveDeviceResourceService',
		                params: me.getItem('frmOper').getValues(),
		                showSuccess: true,
		                callback: function() {
		                    button.setDisabled(false);
		                    me.loadResStore();
		                }
		            })
		        }
		    }
		});
	},
	onBtnDelClick : function(button, e, eOpts) {
		if (!this.getItem('txtDeviceCode').isValid()) {
		    return;
		}
		
		var me = this;
		Ext.Msg.show({
		    title: button.getText(),
		    msg: T.msg.are_you_sure_to_delete_data,
		    icon: Ext.Msg.QUESTION,
		    buttons: Ext.Msg.YESNO,
		    fn: function(btnId) {
		        if ('yes' == btnId) {
		            button.setDisabled(true);
		
		            YX.ZbusRpc.request({
		                module: 'SresSaveDeviceResourceService',
		                params: {
		                    resId: me.getItem('txtDeviceCode').getValue(),
		                    resStatus: 'D'
		                },
		                showSuccess: true,
		                success:function(rsp){
		                  if(!Ext.isEmpty(rsp.fileId)){
		                      Report.exportExcel(rsp.fileId);
		                  }  
		                },
		                callback: function() {
		                    button.setDisabled(false);
		                    me.getItem('frmOper').getForm().reset();
		                    me.loadResStore();
		                }
		            })
		        }
		    }
		});
	},
	onBtnSearchClick : function(button, e, eOpts) {
		this.loadResStore(this.getItem('frmSearch').getValues());
	},
	onBtnResetClick : function(button, e, eOpts) {
		this.getItem('frmSearch').getForm().reset();
		this.getItem('frmOper').getForm().reset();
	},
	onGrdResItemclick : function(dataview, record, item, index, e, eOpts) {
		this.getItem('frmOper').loadRecord(record);
	},
	onCmbShopCodeSelect : function(combo, records, eOpts) {
		this.getItem('cmbLineCode').setValue('');
		this.loadLineStore({shopCode:records[0].get('shopCode')});
	},
	loadResStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ResStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		store.loadPage(1, options);
	},
	loadResTypeStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ResTypeStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		store.loadPage(1, options);
	},
	loadWorkshopStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('WorkshopStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		store.loadPage(1, options);
	},
	loadLineStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('LineStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		store.loadPage(1, options);
	},
	loadStatusStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('StatusStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		store.loadPage(1, options);
	},
	loadUserStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('UserStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		store.loadPage(1, options);
	}
});